<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];

include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

$mode=$_GET['mode'];
if (isset($mode) && $mode == "search"){
	$medicineName = $_GET['medicine_name'];
	$listMedicine = $managerIUDS->listQueryLimit($page, "medicine", "medicine_id"," WHERE medicine_name = '".$medicineName."'");

}else {
	$listMedicine = $managerIUDS->listQueryLimit($page, "medicine", "medicine_id","");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Species List :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script type="text/javascript">
			$(document).ready(function(){ 
			$( "#combobox" ).combobox();
			$( "#toggle" ).click(function() {
				$( "#combobox" ).toggle();
			});
				$("#insert").click(function(){ 
					window.location = "medicine_detail.php?mode=insert";
				}); 
				$("#medicine_name").autocomplete({
					source: function( request, response ) {
		                $.ajax({
							type: "GET",
							url: "../actions/medicine_action.php",
							data: {mode : "listMedicineData",medicine_name : $("#medicine_name").val()},
							contentType: "application/json; charset=utf-8", 
							dataType: "json",
		                    success: function( data ) {
			                        response( $.map( data, function( item ) {
			                            return {
			                                label: item.medicine_name,
			                                value: item.medicine_name
			                            }
			                        }));
		                    }
		                });
		            },
					minLength: 2
				});
			}); 
			function deleteMedicineData(medicineId) {
				if (confirm("Do you really want to delete?")) {
					window.location = "../actions/medicine_action.php?mode=delete&medicine_id=" + medicineId;
				}
			}

			function updateMedicineData(medicineId) {
					window.location = "medicine_detail.php?mode=update&medicine_id=" + medicineId;
			}
		</script>
</head>
<body>
<form name="frm1" id="frm1" action="../actions/medicine_action.php" method="get">
	<table width="100%" border="0">
		<tr>
        	<td align="right">ชื่อยา : 
        		<input type="text" name="medicine_name" id="medicine_name" class="higthInput">
				<input type="submit" value="ค้นหา"/>
			</td>
         </tr>	
	</table>
	<input type="hidden" name="mode" id="mode" value="search">
</form>
<table border="0" width="100%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<td width="1%" align="center">ลำดับ</td>
			<td width="8%" align="center">ชื่อยา</td>
			<td width="10%" align="center">รายละเอียดยา</td>
			<td width="2%" align="center">ราคา</td>
			<td width="2%" align="center">จำนวน</td>
			<td width="4%" align="center">ชนิดของยา</td>
			<td width="6%" align="center">ประเภทของยา</td>
			<td width="1%" align="center">แก้ไข</td>
			<td width="1%" align="center">ลบ</td>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
	<?php
	$i = $listMedicine->startLimit;
	$r = 0;
	$i = ((($page*10)+1)-10);
	if(sizeof($listMedicine) > 0){
		foreach ($listMedicine as $value) {
			?>
		<tr class="ui-widget-content">
			<td width="1%" align="center"><?=$i?></td>
			<td width="8%" align="center"><?=$value["medicine_name"]?></td>
			<td width="10%" align="center"><?=$value["medicine_detail"]?></td>
			<td width="2%" align="center"><?=$value["price"]?></td>
			<td width="2%" align="center"><?=$value["amount"]?></td>
			<td width="6%" align="center"><?php 
			if($value["medicine_sub_type"]=="S"){
				echo "ยาเม็ด";
			}else if($value["medicine_sub_type"]=="S"){
				echo "ยาน้ำ";
			}else if($value["medicine_sub_type"]=="I"){
				echo "ยาฉีด";
			}else if($value["medicine_sub_type"]=="P"){
				echo "ยาทา";
			}
			?>
			</td>
			<td width="6%" align="center"><?php 
			if($value["medicine_type"]=="I"){
				echo "ยาใช้ภายใน";
			}else{
				echo "ยาใช้ภายนอก";
			}
			?>
			</td>
			<td width="1%" align="center">
			<button onclick="updateMedicineData('<?=$value["medicine_id"]?>');">
				<span class="ui-icon ui-icon-pencil" title="edit"></span>
			</button>
			</td>
			<td width="1%" align="center">
			<button onclick="deleteMedicineData('<?=$value["medicine_id"]?>');">
				<span class="ui-icon ui-icon-trash" title="delete"></span>
			</button>
			</td>
		</tr>
		<?php
		$i++; $r++;}
		}else{
			if($mode!="search"){
			?>
			<tr>
				<td colspan="9" align="center"><font size="2" color="red">ไม่มีข้อมูล</font></td>
			</tr>
			<?php
			}else{
			?>
			<tr>
				<td colspan="9" align="center"><font size="2" color="red">ไม่พบข้อมูล</font></td>
			</tr>
			<?php
			}
		}
		?>
		<tr>
			<td align="center" colspan="11"><?=$managerIUDS->buildPageBtn($page, $managerIUDS->getMaxPageByColumnName("medicine", "medicine_id"))?></td>
		</tr>
		<tr>
			<td align="center" colspan="11"><input type="button" name="insert"
				id="insert" value="เพิ่มข้อมูล"></td>
		</tr>
	</tbody>
</table>
</body>
</html>
